import {
	mergeConfig,
	dispatchRequest,
	jsonpRequest
} from './utils.js';
/**
 * 
 */
export default class request {
	/**
	 * @param {Object} options
	 */
	constructor(options) {
		//请求公共地址
		this.baseUrl = options.baseUrl || '';
		//公共文件上传请求地址
		this.fileUrl = options.fileUrl || '';
		//超时时间
		this.timeout = options.timeout || 6000;
		//服务器上传图片默认url
		this.defaultUploadUrl = options.defaultUploadUrl || '';
		//服务器上传文件名称
		this.defaultFileName = options.defaultFileName || '';
		//默认请求头
		this.header = options.header || {};
		//默认配置
		this.config = options.config || {
			isPrompt: true,
			load: true,
			isFactory: true,
			resend: 0
		};
	}
	//post请求
	post(url = '', data = {}, options = {}) {
		return this.request({
			method: 'POST',
			data: data,
			url: url,
			...options
		});
	}
	//get请求
	get(url = '', data = {}, options = {}) {
		return this.request({
			method: 'GET',
			data: data,
			url: url,
			...options
		});
	}
	//put请求
	put(url = '', data = {}, options = {}) {
		return this.request({
			method: 'PUT',
			data: data,
			url: url,
			...options
		});
	}
	//delete请求
	delete(url = '', data = {}, options = {}) {
		return this.request({
			method: 'DELETE',
			data: data,
			url: url,
			...options
		});
	}
	//jsonp请求(只限于H5使用)
	jsonp(url = '', data = {}, options = {}) {
		return this.request({
			method: 'JSONP',
			data: data,
			url: url,
			...options
		});
	}
	/**
	 * 接口请求方法，发起网络请求
	 * @param {Object} data
	 */
	async request(data) {
		//请求数据
		let requestInfo,
			//是否运行过请求开始钩子
			runRequestStart = false;
		try {
			if (!data.url) {
				throw {
					errMsg: '【request】缺失数据url',
					statusCode: 0
				};
			}
			//数据合并
			requestInfo = mergeConfig(this, data);
			//GET请求头部替换
			if (requestInfo.method == 'GET') {
				requestInfo.header = Object.assign({}, requestInfo.header, {
					'Content-type': 'application/json; charset=UTF-8'
				});
			}
			//请求前回调
			if (this.requestStart) {
				//运行过请求开始钩子
				let requestStart = this.requestStart(requestInfo);
				runRequestStart = true;
				if (typeof requestStart == 'object') {
					/**
					 * data：请求数据
					 * header：请求头
					 * isPrompt：自动提示
					 * load：加载动画
					 * isFactory：数据工厂
					 */
					let changekeys = ['data', 'header', 'isPrompt', 'load', 'isFactory'];
					changekeys.forEach(key => {
						requestInfo[key] = requestStart[key];
					});
				} else {
					throw {
						errMsg: '【request】请求开始拦截器未通过',
						statusCode: 0,
						data: requestInfo.data,
						method: requestInfo.method,
						header: requestInfo.header,
						url: requestInfo.url,
					};
				}
			}
			let requestResult = {};
			//
			if (requestInfo.method == 'JSONP') {
				requestResult = await jsonpRequest(requestInfo);
			} else {
				// #ifdef APP-PLUS
				console.log('【dispatchRequest：请求地址】:', requestInfo.url);
				console.log('【dispatchRequest：请求参数】:', requestInfo.data);
				// #endif
				requestResult = await dispatchRequest(requestInfo);
				// #ifdef APP-PLUS
				console.log('【dispatchRequest：请求结果】:', requestResult);
				// #endif
			}
			//是否用外部的数据处理方法
			if (requestInfo.isFactory && this.dataFactory) {
				//数据处理
				let result = await this.dataFactory({
					...requestInfo,
					response: requestResult
				});
				return Promise.resolve(result);
			} else {
				return Promise.resolve(requestResult);
			}
		} catch (err) {
			this.requestError && this.requestError(err);
			return Promise.reject(err);
		} finally {
			//如果请求开始未运行到，请求结束也不运行
			if (runRequestStart) {
				this.requestEnd && this.requestEnd(requestInfo);
			}
		}
	}
}